
import Taro, { useLaunch, useRouter } from '@tarojs/taro';
import { Provider } from 'react-redux';
import { validate } from '@/service/base';
import store from '@/store';
import { setStore } from '@/reducer/common';

import './app.scss';

export default ({ children }) => {
    // 检查登录状态
    const checkLogged = () => {
        validate().then(response => {
            let userInfo = response?.data?.user || {};
            // redux记录用户信息
            store.dispatch(setStore({userInfo}));
        }).catch(e => {
            if(e?.code === '-4') return;
            Taro.showToast({
                title: '服务异常，请稍后重试',
                icon: 'error',
                duration: 1000*10,
            });
        });
    };

    useLaunch(() => {
        checkLogged();
    });

    return <Provider store={store}>
        {children}
    </Provider>
}
